package com.andy.note.boot.config;

import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by chengao on 2020/06/05.
 * https://gitee.com/littleshybee/springboot-forward/tree/master
 */
@Configuration
public class ProxyServletConfiguration {
    // 代理的地址
    private String proxyUrl = "/proxy/*";
    // 转向的地址
    private String targetUrl = "http://www.163.com";

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), proxyUrl);
        //这个setName必须要设置，并且多个的时候，名字需要不一样
        servletRegistrationBean.setName("forward1");
        servletRegistrationBean.addInitParameter("targetUri", targetUrl);
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
        return servletRegistrationBean;
    }

//    @Bean
//    public ServletRegistrationBean servletRegistrationBean2(){
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), proxyUrl2);
//        //这个setName必须要设置，并且多个的时候，名字需要不一样
//        servletRegistrationBean.setName("forward2");
//        servletRegistrationBean.addInitParameter("targetUri", targetUrl2);
//        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
//        return servletRegistrationBean;
//    }

}